package com.sduept.nwld.primaryequipment.rest;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sduept.bigdata.fault.breaker.service.BreakerActionQueryManager;
import com.sduept.bigdata.fault.dto.BreakerActionInfoDTO;
import com.sduept.bigdata.fault.model.CTSaturation;
import com.sduept.bigdata.fault.special.maintenance.manager.CtsaturatedQueryManager;
import com.sduept.bigdata.risk.assessment.model.PrimaryEquipmentCheckInfo;
import com.sduept.bigdata.risk.assessment.model.PrimaryEquipmentCounterMeasuresSubInfo;
import com.sduept.bigdata.risk.assessment.model.PrimaryEquipmentDefectInfo;
import com.sduept.bigdata.risk.service.AssessmentParam;
import com.sduept.bigdata.risk.service.EquipmentAssessmentManager;
import com.sduept.bigdata.risk.service.EquipmentAssessmentManager.EquipmentAssessment;
import com.sduept.bigdata.risk.service.EquipmentAssessmentManager.StationAssessmentModel;
import com.sduept.bigdata.risk.service.EquipmentCheckQueryManager;
import com.sduept.bigdata.risk.service.EquipmentCounterMeasuresQueryManager;
import com.sduept.bigdata.risk.service.EquipmentDefectQueryManager;
import com.sduept.cim.ledger.manager.EquipmentLedgerManager;
import com.sduept.cim.ledger.model.PrimaryEquipmentLedger;
import com.sduept.cim.model.core.Substation;
import com.sduept.cim.model.wires.Breaker;
import com.sduept.cim.model.wires.BusbarSection;
import com.sduept.cim.model.wires.PowerTransformer;
import com.sduept.core.ParamManager;
import com.sduept.core.entity.SystemParameter;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.utils.DateUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping("/primerisk")
public class PrimeRiskEvaluateService {
	@Autowired
	private EquipmentAssessmentManager ea;
	@Autowired
	private CimManager cim;
	@Autowired
	private CtsaturatedQueryManager ctm;
	@Autowired
	private EquipmentLedgerManager elqm;
	@Autowired
	private EquipmentCheckQueryManager ecqm;
	@Autowired
	private EquipmentCounterMeasuresQueryManager ecmqm;
	@Autowired
	private EquipmentDefectQueryManager edqm;
	@Autowired
	private BreakerActionQueryManager baqm;
	@Autowired
	private ParamManager pc;
	/**
	 * 根据站id获取该站的一次设备各项目风险评估
	 * @param
	 * @return {name:[[],[]],name:[]}
	 */
	

	@GetMapping("/getPrimeRiskProject/{stationId}")
	public String getPrimeRiskProject(@PathVariable("stationId") String sid){
		//String[] type ={  "反措未执行", "家族性缺陷", "超期服役", "超期未检", "断路器隐患", "电流互感器饱和", "变压器暂态过载"  };
		EquipmentAssessment equip=ea.getPrimaryEquipmentAssessment(sid);
		JSONObject jso=new JSONObject();
		jso.put("反措未执行",equip.getPrimaryEquipmentCounterMeasuresInfo());
		jso.put("未处理缺陷",equip.getPrimaryEquipmentDefectInfoList());
		jso.put("超期服役",equip.getPrimaryEquipmentLedgerList());
		jso.put("超期未检", equip.getPrimaryEquipmentCheckInfoList());
		jso.put("断路器动作异常", equip.getBreakerActionExceptionList());
		jso.put("CT饱和", equip.getCtSaturationList());
		//jso.put("变压器暂态过载", equip);
		return jso.toString();
	}

	private String[] getPrivateRiskTypeByLevel(String level) {
		if("低风险".equals(level)) {
			return new String[] {"反措未执行", "未处理缺陷"};
		} else if("中等风险".equals(level)) {
			return new String[] { "超期服役", "超期未检",};
		} else if("高风险".equals(level)) {
			return new String[] {"断路器动作异常", "CT饱和"};
		} else {
			return new String[] {};
		}
	}
	
	/**
	 * 获取单个变电站的评估数据
	 * @param sid
	 * @return
	 */
	private String getPieChartOptionData(String sid) {
		//获取变电站一次设备数量
		List<PowerTransformer> ps = cim.getPowerTransformersByStationId(sid);
		List<Breaker> bs = cim.getBreakersByStationIdAndVoltagelevel(sid, 500);
		List<BusbarSection> bus = cim.getBusbarSectionsByStationIdAndStandVoltagelevel(sid, 500);
		int pcount = ps.size();
		int bcount = bs.size();
		int buscount = bus.size();
		int ctcount = bcount;
		int total = pcount+bcount+buscount+ctcount;
		JSONObject countJso=new JSONObject();
		countJso.put("total",total);
		countJso.put("变压器",pcount);
		countJso.put("母线",buscount);
		countJso.put("断路器", bcount);
		countJso.put("CT", ctcount);
		
		//获取影响风险等级的各个具体的记录列表
		EquipmentAssessment equip=ea.getPrimaryEquipmentAssessment(sid);
		JSONObject typeCountjso=new JSONObject();
		
		List<PrimaryEquipmentCounterMeasuresSubInfo> fcs = equip.getPrimaryEquipmentCounterMeasuresInfo();
		Map<String,List<PrimaryEquipmentCounterMeasuresSubInfo>> fcInfos=this.generatePrimaryEquipmentCounterMeasuresSubInfo(fcs);
		typeCountjso.put("反措未执行",fcInfos);
		
		List<PrimaryEquipmentDefectInfo> defects = equip.getPrimaryEquipmentDefectInfoList();
		Map<String,List<PrimaryEquipmentDefectInfo>> defectInfos=this.generatePrimaryEquipmentDefectInfo(defects);
		typeCountjso.put("未处理缺陷",defectInfos);
		
		List<PrimaryEquipmentLedger> ledger = equip.getPrimaryEquipmentLedgerList();
		Map<String,List<PrimaryEquipmentLedger>> ledgerInfos=this.generatePrimaryEquipmentLedger(ledger);
		typeCountjso.put("超期服役",ledgerInfos);
		
		List<PrimaryEquipmentCheckInfo> checks = equip.getPrimaryEquipmentCheckInfoList();
		Map<String,List<PrimaryEquipmentCheckInfo>> checkInfos=this.generatePrimaryEquipmentCheckInfo(checks);
		typeCountjso.put("超期未检", checkInfos);
		
		List<BreakerActionInfoDTO> breakers = equip.getBreakerActionExceptionList();
		Map<String,List<BreakerActionInfoDTO>> breakerInfos=this.generateBreakerException(breakers);
		typeCountjso.put("断路器动作异常", breakerInfos);
		
		
		List<CTSaturation> cts = equip.getCtSaturationList();
		Map<String,List<CTSaturation>> ctInfos=this.generateCTSA(cts);
		typeCountjso.put("CT饱和", ctInfos);
		 
		/*List<CTSaturation> trans = equip.getCtSaturationList();
		Map<String,List<CTSaturation>> transInfos=this.generateTrans(cts);
		typeCountjso.put("变压器暂态过载", transInfos);*/
		
		JSONObject jso=new JSONObject();
		//主饼图数据[{name:xxx,value:1}]
		JSONArray pieMainValues = new JSONArray();
		//外围饼图数据[{name:xxx,value:1}],顺序要和主饼图保持一致
		JSONArray pieSubValues = new JSONArray();
		String conclusion="共有一次设备"+total+"个       ";
		//右边说明{"高风险":{"CT饱和":0,"断路器动作异常":1},"低风险":{"超期未检":1}}
		JSONObject descJso=new JSONObject();
		for(String g : EquipmentAssessmentManager.PRIME_RISK_GROUP) {
			Map<String,Integer> descMap = new HashMap<>();
			JSONObject tmp = new JSONObject();
			String[] types = getPrivateRiskTypeByLevel(g);
			int count = 0;
			if("正常".equals(g)) {
				count = total -fcInfos.size()-defectInfos.size()-ledgerInfos.size()-checkInfos.size()-breakerInfos.size()
							-ctInfos.size();
				JSONObject tmpsub = new JSONObject();
				tmpsub.put("name", g);
				tmpsub.put("value", count);
				tmpsub.put("itemStyle", getGroupColor(g));
				pieSubValues.add(tmpsub);
			} else {
				for(String ts : types) {
					Object tclist = typeCountjso.get(ts);
					if(tclist != null && tclist instanceof Map) {
						int cttmp = ((Map)typeCountjso.get(ts)).size();
						if(cttmp!=0){
							count += cttmp;
							JSONObject tmpsub = new JSONObject();
							tmpsub.put("name", ts);
							tmpsub.put("value", cttmp);
							pieSubValues.add(tmpsub);
						}
						descMap.put(ts, cttmp);
					}
					descJso.put(g, descMap);
				}
			}
			if(count!=0){
				tmp.put("name", g);
				tmp.put("value", count);
				if("高风险".equals(g)) {
					tmp.put( "selected",true);
				}
				tmp.put("itemStyle", getGroupColor(g));
				conclusion+=g+count+"个       ";
				pieMainValues.add(tmp);
			}
		}
		jso.put("pieMain", pieMainValues);
		jso.put("pieSub", pieSubValues);
		jso.put("subDatas", typeCountjso);
		jso.put("counts", countJso);
		jso.put("conclusion", conclusion);
		jso.put("breakExpParam", getBreakerExpParams());
		jso.put("desc", descJso);//这里放等级因素说明
		return jso.toString();
	}
	/**
	 * 获取风险评估各个因素的统计总数
	 * @return 
	 */
	private JSONObject getTotalRiskTypeCount() {
		JSONObject jso = new JSONObject();
		for(String s : EquipmentAssessmentManager.PRIME_RISK_TYPE) {
			switch (s) {
			case "反措未执行":
				jso.put(s, ea.getCounterMeasures().size());
				break;
			case "未处理缺陷":
				jso.put(s, ea.getDefects().size());
				break;
			case "超期服役":
				jso.put(s, ea.getLedgers().size());
				break;
			case "超期未检":
				jso.put(s, ea.getEquipmentChecks().size());
				break;
			case "断路器动作异常":
				jso.put(s, ea.countBreakerActions());
				break;
			case "CT饱和":
				jso.put(s, ea.getCTSaturations().size());
				break;
			case "变压器暂态过载":
				//jso.put(s, ea.countTransformExceptions());
				break;
			default:
				break;
			}
		}
		return jso;
	}
	private JSONObject getGroupColor(String g) {
		JSONObject normal = new JSONObject();
		JSONObject color = new JSONObject();
		for (int i = 0; i < EquipmentAssessmentManager.PRIME_RISK_GROUP.length; i++) {
			if(EquipmentAssessmentManager.PRIME_RISK_GROUP[i].equals(g)) {
				color.put("color", EquipmentAssessmentManager.PRIME_RISK_GROUP_COLOR[i]) ;
				break;
			}
		}
		normal.put("normal", color);
		return normal;
	}
	private String getMapPieChartOptionData(String sid) {
		//获取变电站一次设备数量
		List<PowerTransformer> ps = cim.getPowerTransformersByStationId(sid);
		List<Breaker> bs = cim.getBreakersByStationIdAndVoltagelevel(sid, 500);
		List<BusbarSection> bus = cim.getBusbarSectionsByStationIdAndStandVoltagelevel(sid, 500);
		int pcount = ps.size();
		int bcount = bs.size();
		int buscount = bus.size();
		int ctcount = bcount;
		int total = pcount+bcount+buscount+ctcount;
		//获取影响风险等级的各个具体的记录列表
		EquipmentAssessment equip=ea.getPrimaryEquipmentAssessment(sid);
		JSONObject typeCountjso=new JSONObject();
		
		List<PrimaryEquipmentCounterMeasuresSubInfo> fcs = equip.getPrimaryEquipmentCounterMeasuresInfo();
		Map<String,List<PrimaryEquipmentCounterMeasuresSubInfo>> fcInfos=this.generatePrimaryEquipmentCounterMeasuresSubInfo(fcs);
		typeCountjso.put("反措未执行",fcInfos);
		
		List<PrimaryEquipmentDefectInfo> defects = equip.getPrimaryEquipmentDefectInfoList();
		Map<String,List<PrimaryEquipmentDefectInfo>> defectInfos=this.generatePrimaryEquipmentDefectInfo(defects);
		typeCountjso.put("未处理缺陷",defectInfos);
		
		List<PrimaryEquipmentLedger> ledger = equip.getPrimaryEquipmentLedgerList();
		Map<String,List<PrimaryEquipmentLedger>> ledgerInfos=this.generatePrimaryEquipmentLedger(ledger);
		typeCountjso.put("超期服役",ledgerInfos);
		
		List<PrimaryEquipmentCheckInfo> checks = equip.getPrimaryEquipmentCheckInfoList();
		Map<String,List<PrimaryEquipmentCheckInfo>> checkInfos=this.generatePrimaryEquipmentCheckInfo(checks);
		typeCountjso.put("超期未检", checkInfos);
		
		List<BreakerActionInfoDTO> breakers = equip.getBreakerActionExceptionList();
		Map<String,List<BreakerActionInfoDTO>> breakerInfos=this.generateBreakerException(breakers);
		typeCountjso.put("断路器动作异常", breakerInfos);
		
		
		List<CTSaturation> cts = equip.getCtSaturationList();
		Map<String,List<CTSaturation>> ctInfos=this.generateCTSA(cts);
		typeCountjso.put("CT饱和", ctInfos);
		 
		/*List<CTSaturation> trans = equip.getCtSaturationList();
		Map<String,List<CTSaturation>> transInfos=this.generateTrans(trans);
		typeCountjso.put("变压器暂态过载", transInfos);*/
		
		//主饼图数据[{name:xxx,value:1}]
		JSONArray pieMainValues = new JSONArray();
		for(String g : EquipmentAssessmentManager.PRIME_RISK_GROUP) {
			JSONObject tmp = new JSONObject();
			
			String[] types = getPrivateRiskTypeByLevel(g);
			int count = 0;
			if("正常".equals(g)) {
				count = total -fcInfos.size()-defectInfos.size()-ledgerInfos.size()-checkInfos.size()-breakerInfos.size()
						-ctInfos.size();
			} else {
				for(String ts : types) {
					Object tclist = typeCountjso.get(ts);
					if(tclist != null && tclist instanceof Map) {
						int cttmp = ((Map)typeCountjso.get(ts)).size();
						count += cttmp;
					}
				}
			}
				tmp.put("name", g);
				tmp.put("value", count);
				tmp.put("itemStyle", getGroupColor(g));
				pieMainValues.add(tmp);
		}
		return pieMainValues.toString();
	}
	/**
	 * 根据站点获取一次设备总的数量 变压器，断路器，CT(暂时取断路器的数量)，母线
	 * @param sid
	 * @return
	 */

	@GetMapping("/getPrimeRiskProject4PieChart/{stationId}")
	public String getPrimeRiskProject4PieChart(@PathVariable("stationId") String sid){
		return getPieChartOptionData(sid);
	}
	//获取所有的等级，等级内的设备数量，等级内的风险类型，每个风险类型包含的风险数量
	
	/**
	 * 全网一次设备风险评估站点风险等级
	 * @param
	 * @return
	 */

	@GetMapping("/getPrimeRiskLevel")
	public String getPrimeRiskLevel(){
		Collection<StationAssessmentModel> result=ea.getStationPrimaryEquipmentAssessmentResult();
		JSONObject jso = new JSONObject();
		JSONArray rtn = new JSONArray();
		for (StationAssessmentModel s : result) {
			JSONObject jo = new JSONObject();
			JSONObject jo1 = new JSONObject();
			jo1.put("latitude", s.getLatitude());
			jo1.put("longitude", s.getLongitude());
			jo1.put("name", s.getStationName());
			jo1.put("id", s.getStationId());
			jo.put("station", jo1);
			jo.put("level", s.getLevel());
			jo.put("style", s.getStyle());
			jo.put("info", getMapPieChartOptionData(s.getStationId()));
			rtn.add(jo);
		}
		jso.put("stations", rtn);
		jso.put("count", getTotalRiskTypeCount());
		jso.put("levelCount", EquipmentAssessmentManager.PRIME_RISK_GROUP.length);
		jso.put("levelColor", EquipmentAssessmentManager.PRIME_RISK_GROUP_COLOR);
		return jso.toString();
	}
	/**
	 * 全网二次设备异常统计，用在风险告警处,统计各种异常类型的个数(暂时忽略通信中断)
	 * 
	 * @param
	 * @return
	 */

	@GetMapping("/getPrimeExpCount")
	public String getPrimeExpCount() {
		return getTotalRiskTypeCount().toString();
	}
	/**
	 * 按评估类型找站点
	 * @param
	 * @return
	 */

	@GetMapping("/getPrimeRiskStationByType/{type}")
	public String getPrimeRiskStationByType(@PathVariable("type") String type){
		List<StationAssessmentModel> result=ea.getPrimaryRiskInfoByType(type);
		JSONArray rtn = new JSONArray();
		for (StationAssessmentModel s : result) {
			JSONObject jo = new JSONObject();
			JSONObject jo1 = new JSONObject();
			jo1.put("latitude", s.getLatitude());
			jo1.put("longitude", s.getLongitude());
			jo1.put("name", s.getStationName());
			jo1.put("id", s.getStationId());
			jo.put("station", jo1);
			jo.put("style",s.getStyle());
			rtn.add(jo);
		}
		return rtn.toString();
	}
	/**
	 * 按评估类型和站的id找所有信息
	 * @param
	 * @return
	 */

	@GetMapping("/getPrimeInfoByType/{type}/{stationId}")
	public String getInfoByType(@PathVariable("type") String type,@PathVariable("stationId") String stationId){
		return this.getPrimaryInfoByType(type,stationId).toString();
	}
	
	
	/**
	 * 按类型和站点的id统计一次设备风险评估详细信息
	 * */
	private JSONObject getPrimaryInfoByType(String type,String stationId){
		Date[] ds = getStartEndDate();
		Date start = ds[0];
		Date end = ds[1];
		JSONObject jso=new JSONObject();
		List<PowerTransformer> ps = cim.getPowerTransformersByStationId(stationId);
		List<Breaker> bs = cim.getBreakersByStationIdAndVoltagelevel(stationId, 500);
		List<BusbarSection> bus = cim.getBusbarSectionsByStationIdAndStandVoltagelevel(stationId, 500);
		int pcount = ps.size();
		int bcount = bs.size();
		int buscount = bus.size();
		int ctcount = bcount;
		int total = pcount+bcount+buscount+ctcount;
		switch(type){
		case "primeTypeservice"://超期服役
			List<PrimaryEquipmentLedger> overdueRunList = elqm.getOverduePrimaryEquipmentLedgerByStationId(stationId);
			Map<String,List<PrimaryEquipmentLedger>> overdueRunLists=new HashMap<String,List<PrimaryEquipmentLedger>>();
			if(overdueRunList!=null && !overdueRunList.isEmpty()){
				Set<String> odrnames=new HashSet<String>();
				for(PrimaryEquipmentLedger _overdueRunList:overdueRunList){
					if(_overdueRunList.getName()!=null){
						odrnames.add(_overdueRunList.getName());
					}
				}				
				if(!odrnames.isEmpty()){
					for(String odrname:odrnames){
						List<PrimaryEquipmentLedger> _overdueRunLists=new ArrayList<PrimaryEquipmentLedger>();
						for(PrimaryEquipmentLedger __overdueRunLists:overdueRunList){
							if(__overdueRunLists.getName()!=null && __overdueRunLists.getName().equals(odrname)){
								_overdueRunLists.add(__overdueRunLists);
							}
						}						
						overdueRunLists.put(odrname,_overdueRunLists);						
					}
				}
			}
		    jso.put("count", total);
		    jso.put("info", overdueRunLists);
		    break;
		case "primeTypecheck"://超期未检
			List<PrimaryEquipmentCheckInfo> overdueCheckList = ecqm.getPrimaryEquipmentOverdueCheckInfoByStationId(stationId,start,end);
			Map<String,List<PrimaryEquipmentCheckInfo>> overdueCheckLists=new HashMap<String,List<PrimaryEquipmentCheckInfo>>();
			if(overdueCheckList!=null && !overdueCheckList.isEmpty()){
				Set<String> oc_names=new HashSet<String>();
				for(PrimaryEquipmentCheckInfo _overdueCheckList:overdueCheckList){
					if(_overdueCheckList.getDeviceName()!=null){
						oc_names.add(_overdueCheckList.getDeviceName());
					}
				}				
				if(!oc_names.isEmpty()){
					for(String oc_name:oc_names){
						List<PrimaryEquipmentCheckInfo> _overdueCheckLists=new ArrayList<PrimaryEquipmentCheckInfo>();
						for(PrimaryEquipmentCheckInfo __overdueCheckList:overdueCheckList){
							if(__overdueCheckList.getDeviceName()!=null && __overdueCheckList.getDeviceName().equals(oc_name)){
								_overdueCheckLists.add(__overdueCheckList);
							}
						}						
						overdueCheckLists.put(oc_name,_overdueCheckLists);						
					}
				}
			}
		    jso.put("info", overdueCheckLists);  
			jso.put("count", total);
		    break;
		case "primeTypecounter"://超期反措
			List<PrimaryEquipmentCounterMeasuresSubInfo>  overdueCounterMeasuresList = ecmqm.getPrimaryEquipmentOverdueCounterMeasuresByStationId(stationId,start,end);
			Map<String,List<PrimaryEquipmentCounterMeasuresSubInfo>> overdueCounterMeasuresLists=new HashMap<String,List<PrimaryEquipmentCounterMeasuresSubInfo>>();
			if(overdueCounterMeasuresList!=null && !overdueCounterMeasuresList.isEmpty()){
				Set<String> ocm_names=new HashSet<String>();
				for(PrimaryEquipmentCounterMeasuresSubInfo _overdueCounterMeasuresList:overdueCounterMeasuresList){
					if(_overdueCounterMeasuresList.getDeviceName()!=null){
						ocm_names.add(_overdueCounterMeasuresList.getDeviceName());
					}
				}				
				if(!ocm_names.isEmpty()){
					for(String ocm_name:ocm_names){
						List<PrimaryEquipmentCounterMeasuresSubInfo> _overdueCounterMeasuresLists=new ArrayList<PrimaryEquipmentCounterMeasuresSubInfo>();
						for(PrimaryEquipmentCounterMeasuresSubInfo __overdueCounterMeasuresList:overdueCounterMeasuresList){
							if(__overdueCounterMeasuresList.getDeviceName()!=null && __overdueCounterMeasuresList.getDeviceName().equals(ocm_name)){
								_overdueCounterMeasuresLists.add(__overdueCounterMeasuresList);
							}
						}						
						overdueCounterMeasuresLists.put(ocm_name,_overdueCounterMeasuresLists);						
					}
				}
			}
			jso.put("info", overdueCounterMeasuresLists);  
			jso.put("count", total);
		    break;
		case "primeTypedefect"://未处理缺陷
			List<PrimaryEquipmentDefectInfo> defectList = edqm.getUnsolvePrimaryEquipmentDefectInfoByStationId(stationId,start,end);
			Map<String,List<PrimaryEquipmentDefectInfo>> defectLists=new HashMap<String,List<PrimaryEquipmentDefectInfo>>();
			if(defectList!=null && !defectList.isEmpty()){
				Set<String> equ_names=new HashSet<String>();
				for(PrimaryEquipmentDefectInfo _defectList:defectList){
					if(_defectList.getDeviceName()!=null){
						equ_names.add(_defectList.getDeviceName());
					}
				}				
				if(!equ_names.isEmpty()){
					for(String equ_name:equ_names){
						List<PrimaryEquipmentDefectInfo> _defectLists=new ArrayList<PrimaryEquipmentDefectInfo>();
						for(PrimaryEquipmentDefectInfo __defectList:defectList){
							if(__defectList.getDeviceName()!=null&&__defectList.getDeviceName().equals(equ_name)){
								_defectLists.add(__defectList);
							}
						}						
						defectLists.put(equ_name,_defectLists);						
					}
				}
			}
			jso.put("count", total);
		    jso.put("info", defectLists); 
		    break;
		case "primeTypectsa"://CT饱和   按故障线路分组统计返回
			List<CTSaturation> ctList = ctm.getCTSaturationByStationId(stationId,start, end);
			Map<String,List<CTSaturation>> ctLists=new HashMap<String,List<CTSaturation>>();
			if(ctList!=null && !ctList.isEmpty()){
				Set<String> fault_line_names=new HashSet<String>();
				for(CTSaturation _ctList:ctList){
					if(_ctList.getLineName()!=null){
						fault_line_names.add(_ctList.getLineName());
					}
				}				
				if(!fault_line_names.isEmpty()){
					for(String fault_line_name:fault_line_names){
						List<CTSaturation> _ctLists=new ArrayList<CTSaturation>();
						for(CTSaturation __ctList:ctList){
							if(__ctList.getLineName()!=null&&__ctList.getLineName().equals(fault_line_name)){
								_ctLists.add(__ctList);
							}
						}						
						ctLists.put(fault_line_name,_ctLists);						
					}
				}
			}
			jso.put("count", total);
		    jso.put("info", ctLists); 
		    break;
		case "primeTypebreakerExp"://断路器动作异常
			List<BreakerActionInfoDTO> breakerExceptionList = baqm.getStationBreakerActionExceptionInfo(stationId,start,end);
			Map<String,List<BreakerActionInfoDTO>> beLists=new HashMap<String,List<BreakerActionInfoDTO>>();
			if(breakerExceptionList!=null && !breakerExceptionList.isEmpty()){
				Set<String> break_names=new HashSet<String>();
				for(BreakerActionInfoDTO _breakerExceptionList:breakerExceptionList){
					if(_breakerExceptionList.getBreakerName()!=null){
						break_names.add(_breakerExceptionList.getBreakerName());
					}
				}				
				if(!break_names.isEmpty()){
					for(String break_name:break_names){
						List<BreakerActionInfoDTO> _beLists=new ArrayList<BreakerActionInfoDTO>();
						for(BreakerActionInfoDTO __breakerExceptionList:breakerExceptionList){
							if(__breakerExceptionList.getBreakerName()!=null&&__breakerExceptionList.getBreakerName().equals(break_name)){
								_beLists.add(__breakerExceptionList);
							}
						}						
						beLists.put(break_name,_beLists);						
					}
				}
			}
			jso.put("count", bcount);
		    jso.put("info", beLists); 
		    jso.put("param", getBreakerExpParams());
		    break;
		default:
			break;
	   }
		return jso;
	}
	
	/**
	 * 获取断路器动作异常的边界值
	 * @return
	 */
	private Map<String,String> getBreakerExpParams() {
		SystemParameter pa1 = pc.get(AssessmentParam.BREAKER_ACTION_MAX_VALUE);
		SystemParameter pa2 = pc.get(AssessmentParam.BREAKER_ACTION_ARC_MAX_VALUE);
		SystemParameter pa3 = pc.get(AssessmentParam.BREAKER_ACTION_COSS0_MAX_VALUE);
		Map<String,String> rtn=new HashMap<>();
		rtn.put("xh", pa2.getValue());
		rtn.put("act", pa1.getValue());
		rtn.put("cross0", pa3.getValue());
		return rtn;
	}
	
	private Date[] getStartEndDate(){
		Date[] ds = new Date[2];
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		String startDateStr = year+"-01-01 00:00:00";
		String endDateStr = DateUtil.getStringDate(new Date(), DateUtil.pattern_ymd)+" 23:59:59";
		
		Date start = DateUtil.getDateByString(startDateStr, DateUtil.pattern_ymdhms);
		Date end = DateUtil.getDateByString(endDateStr, DateUtil.pattern_ymdhms);
		ds[0]=start;
		ds[1]=end;
		return ds;
	}
	
	/**
	 * 全网CT饱和评估
	 * @param
	 * @return
	 */

	@GetMapping("/getCTAssessment/{year}")
	public String getCTAssessment(@PathVariable("year") int year){
		JSONObject jso=new JSONObject();
		//获取全网CT数目
		List<Breaker> breakers=cim.getBreakerByStandVoltagelevel(500);
		//获取发生的CT饱和数量
		List<CTSaturation> ctsa=ea.getCTSaturationsByYear(year);
		Map<String, List<CTSaturation>> ctMap=generateCTSA(ctsa);
		//全网发生CT饱和站
		Map<String,Integer> ctsaturatedMap = ea.ctsaStationMapByYear(year);
		//获取站点数目
		List<Substation> stations=cim.listStationsByVoltage(500);
		jso.put("totalCT", breakers.size());//全网CT总数(取断路器个数)
		jso.put("ctsaMap", ctMap);//CT饱和的设备数目
		jso.put("csStation", ctsaturatedMap);//发生CT饱和的站点
		jso.put("totalSt", stations.size());//厂站数目
		return jso.toString();
	}
	
	/**
	 * 构造一次设备超期未检Map集合
	 * @param checks
	 * @return
	 */
	private Map<String, List<PrimaryEquipmentCheckInfo>> generatePrimaryEquipmentCheckInfo(
			List<PrimaryEquipmentCheckInfo> checks) {
		Map<String,List<PrimaryEquipmentCheckInfo>> peLists=new HashMap<String,List<PrimaryEquipmentCheckInfo>>();
		if(checks!=null && !checks.isEmpty()){
			Set<String> protectionNames=new HashSet<String>();
			for(PrimaryEquipmentCheckInfo _protectExceptionList:checks){
				protectionNames.add(_protectExceptionList.getDeviceName());
			}				
			if(!protectionNames.isEmpty()){
				for(String protectionName:protectionNames){
					List<PrimaryEquipmentCheckInfo> _protectExceptionLists=new ArrayList<PrimaryEquipmentCheckInfo>();
					for(PrimaryEquipmentCheckInfo __protectExceptionList:checks){
						if(__protectExceptionList.getDeviceName().equals(protectionName)){
							_protectExceptionLists.add(__protectExceptionList);
						}
					}						
					peLists.put(protectionName,_protectExceptionLists);						
				}
			}
		}		
		return peLists;
	}
	
	/**
	 * 构造一次设备反措未执行Map集合
	 * @param ledger
	 * @return
	 */
	private Map<String, List<PrimaryEquipmentCounterMeasuresSubInfo>> generatePrimaryEquipmentCounterMeasuresSubInfo(
			List<PrimaryEquipmentCounterMeasuresSubInfo> ledger) {
		Map<String,List<PrimaryEquipmentCounterMeasuresSubInfo>> peLists=new HashMap<String,List<PrimaryEquipmentCounterMeasuresSubInfo>>();
		if(ledger!=null && !ledger.isEmpty()){
			Set<String> protectionNames=new HashSet<String>();
			for(PrimaryEquipmentCounterMeasuresSubInfo _protectExceptionList:ledger){
				protectionNames.add(_protectExceptionList.getDeviceName());
			}				
			if(!protectionNames.isEmpty()){
				for(String protectionName:protectionNames){
					List<PrimaryEquipmentCounterMeasuresSubInfo> _protectExceptionLists=new ArrayList<PrimaryEquipmentCounterMeasuresSubInfo>();
					for(PrimaryEquipmentCounterMeasuresSubInfo __protectExceptionList:ledger){
						if(__protectExceptionList.getDeviceName().equals(protectionName)){
							_protectExceptionLists.add(__protectExceptionList);
						}
					}						
					peLists.put(protectionName,_protectExceptionLists);						
				}
			}
		}		
		return peLists;
	}
	
	/**
	 * 构造一次设备未处理缺陷Map集合
	 * @param defects
	 * @return
	 */
	private Map<String, List<PrimaryEquipmentDefectInfo>> generatePrimaryEquipmentDefectInfo(
			List<PrimaryEquipmentDefectInfo> defects) {
		Map<String,List<PrimaryEquipmentDefectInfo>> peLists=new HashMap<String,List<PrimaryEquipmentDefectInfo>>();
		if(defects!=null && !defects.isEmpty()){
			Set<String> protectionNames=new HashSet<String>();
			for(PrimaryEquipmentDefectInfo _protectExceptionList:defects){
				protectionNames.add(_protectExceptionList.getDeviceName());
			}				
			if(!protectionNames.isEmpty()){
				for(String protectionName:protectionNames){
					List<PrimaryEquipmentDefectInfo> _protectExceptionLists=new ArrayList<PrimaryEquipmentDefectInfo>();
					for(PrimaryEquipmentDefectInfo __protectExceptionList:defects){
						if(__protectExceptionList.getDeviceName().equals(protectionName)){
							_protectExceptionLists.add(__protectExceptionList);
						}
					}						
					peLists.put(protectionName,_protectExceptionLists);						
				}
			}
		}		
		return peLists;
	}
	
	/**
	 * 构建一次设备超期服役Map集合
	 * @param fcs
	 * @return
	 */
	private Map<String, List<PrimaryEquipmentLedger>> generatePrimaryEquipmentLedger(
			List<PrimaryEquipmentLedger> fcs) {
		Map<String,List<PrimaryEquipmentLedger>> peLists=new HashMap<String,List<PrimaryEquipmentLedger>>();
		if(fcs!=null && !fcs.isEmpty()){
			Set<String> protectionNames=new HashSet<String>();
			for(PrimaryEquipmentLedger _protectExceptionList:fcs){
				protectionNames.add(_protectExceptionList.getName());
			}				
			if(!protectionNames.isEmpty()){
				for(String protectionName:protectionNames){
					List<PrimaryEquipmentLedger> _protectExceptionLists=new ArrayList<PrimaryEquipmentLedger>();
					for(PrimaryEquipmentLedger __protectExceptionList:fcs){
						if(__protectExceptionList.getName().equals(protectionName)){
							_protectExceptionLists.add(__protectExceptionList);
						}
					}						
					peLists.put(protectionName,_protectExceptionLists);						
				}
			}
		}		
		return peLists;
	}	
	
	/**
	 * 构建断路器动作异常Map集合
	 * @param fcs
	 * @return
	 */
	private Map<String, List<BreakerActionInfoDTO>> generateBreakerException(
			List<BreakerActionInfoDTO> fcs) {
		Map<String,List<BreakerActionInfoDTO>> peLists=new HashMap<String,List<BreakerActionInfoDTO>>();
		if(fcs!=null && !fcs.isEmpty()){
			Set<String> protectionNames=new HashSet<String>();
			for(BreakerActionInfoDTO _protectExceptionList:fcs){
				protectionNames.add(_protectExceptionList.getBreakerName());
			}				
			if(!protectionNames.isEmpty()){
				for(String protectionName:protectionNames){
					List<BreakerActionInfoDTO> _protectExceptionLists=new ArrayList<BreakerActionInfoDTO>();
					for(BreakerActionInfoDTO __protectExceptionList:fcs){
						if(__protectExceptionList.getBreakerName().equals(protectionName)){
							_protectExceptionLists.add(__protectExceptionList);
						}
					}						
					peLists.put(protectionName,_protectExceptionLists);						
				}
			}
		}		
		return peLists;
	}	
	/**
	 * 构建CT饱和Map集合
	 * @param fcs
	 * @return
	 */
	private Map<String, List<CTSaturation>> generateCTSA(
			List<CTSaturation> fcs) {
		Map<String,List<CTSaturation>> peLists=new HashMap<String,List<CTSaturation>>();
		if(fcs!=null && !fcs.isEmpty()){
			Set<String> protectionNames=new HashSet<String>();
			for(CTSaturation _protectExceptionList:fcs){
				protectionNames.add(_protectExceptionList.getLineName());
			}				
			if(!protectionNames.isEmpty()){
				for(String protectionName:protectionNames){
					List<CTSaturation> _protectExceptionLists=new ArrayList<CTSaturation>();
					for(CTSaturation __protectExceptionList:fcs){
						if(__protectExceptionList.getLineName().equals(protectionName)){
							_protectExceptionLists.add(__protectExceptionList);
						}
					}						
					peLists.put(protectionName,_protectExceptionLists);						
				}
			}
		}		
		return peLists;
	}	
	/**
	 * 构建变压器暂态Map集合
	 * @param fcs
	 * @return
	 */
	private Map<String, List<CTSaturation>> generateTrans(List<CTSaturation> fcs) {
		Map<String,List<CTSaturation>> peLists=new HashMap<String,List<CTSaturation>>();
		if(fcs!=null && !fcs.isEmpty()){
			Set<String> protectionNames=new HashSet<String>();
			for(CTSaturation _protectExceptionList:fcs){
				protectionNames.add(_protectExceptionList.getLineName());
			}				
			if(!protectionNames.isEmpty()){
				for(String protectionName:protectionNames){
					List<CTSaturation> _protectExceptionLists=new ArrayList<CTSaturation>();
					for(CTSaturation __protectExceptionList:fcs){
						if(__protectExceptionList.getLineName().equals(protectionName)){
							_protectExceptionLists.add(__protectExceptionList);
						}
					}						
					peLists.put(protectionName,_protectExceptionLists);						
				}
			}
		}		
		return peLists;
	}	
}
